﻿using System;
using System.Collections;
using Portal.Data.Entity;
using Portal.Data.Webparts;
using Portal.Core;
using Telerik.WebControls;

public partial class Webparts_Misc_SiteMap : WebpartControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            LoadTree();
        }
    }

    private void LoadTree()
    {
        tree1.Nodes.Clear();

        //có thể có nhiều gốc trong cùng 1 ngôn ngữ
        Category root = Category.FindFirstRoot();
        RadTreeNode childnode = new RadTreeNode(root.FullName, root.ID.ToString());
        childnode.NavigateUrl = root.Url;

        tree1.Nodes.Add(childnode);

        AddChildNodes(childnode);
        tree1.ExpandAllNodes();
    }

    private void AddChildNodes(RadTreeNode node)
    {
        Category catNode = Category.FindByPrimaryKey(node.Value);
        IList listChild = Category.FindSubCategories(catNode.ID);
        //        IList listChild = catNode.Childrens;
        if (listChild.Count > 0)
        {
            foreach (Category child in listChild)
            {
                if (child.CateType == (int)ObjectTypeClass.ObjectType.Other)
                    continue;
                RadTreeNode childnode = new RadTreeNode(child.FullName, child.ID.ToString());
                childnode.NavigateUrl = child.Url;
                AddChildNodes(childnode);

                node.Nodes.Add(childnode);
            }
        }
    }
}